<?php
/**
 * @file
 * Download Monitor - Allows you to trace file downloads. Just place the direct link
 * to the file on the web, and specify the extensions of files you want to trace in the GUI.
 * It creates interactive chart and records downloads for today, yesterday, this week,
 * last week, this month, last month and total.
 * Requires writable .htaccess file of Apache Web server in Joomla root folder.
 * @package download-monitor
 * @version 1.0
 * @revision @REVISION@
 * @license http://www.gnu.org/licenses/gpl-3.0.txt     GNU General Public License v3
 * @copyright (C) 2012 by CodeGravity.com - All rights reserved!
 * @website http://www.codegravity.com
 */

if($task=='edit')
{
	$extensionquery_edit = sprintf("SELECT * FROM #__download_monitor_extension where eid='%d'",(int)$eid);
	$db->setQuery($extensionquery_edit);
	$extensionar_edit = $db->loadRow();
	$editextname = $extensionar_edit[1];
	
	if (isset($_POST['Submit']) && $_POST['Submit']=="Update")
	{
		$extname = trim(mysql_escape_string($_POST['extname']));

		$extensionquery_ht_prev = sprintf("SELECT * FROM #__download_monitor_extension");
		$db->setQuery($extensionquery_ht_prev);
		$extensionar_ht_prev = $db->loadRowList();
		
		foreach($extensionar_ht_prev as $extensionhtprev)
		{
			$ext_n_prev = $ext_n_prev.$extensionhtprev[1]."|";
		}
		$ext_n_prev = substr($ext_n_prev,0,strlen($ext_n_prev)-1);			
		
		$extensionquery = sprintf("update #__download_monitor_extension set extname='%s' where eid='%d'", mysql_escape_string($extname), (int) $eid);
		$db->setQuery($extensionquery);
		$db->query();
		
		$extensionquery_ht = sprintf("SELECT * FROM #__download_monitor_extension");
		$db->setQuery($extensionquery_ht);
		$extensionar_ht = $db->loadRowList();
		
		foreach($extensionar_ht as $extensionht)
		{
			$ext_n = $ext_n.$extensionht[1]."|";
		}
		$ext_n = substr($ext_n,0,strlen($ext_n)-1); 
		
		$findpath = (JURI::root());
		$p = parse_url( $findpath );
		$path = $p['path']; 

		
		$writingonht_prev = "RewriteEngine on"."\n"."RewriteRule ^(.*).(".$ext_n_prev.")$ ".$path."index.php?option=com_download-monitor&file=$1.$2 [R,L]";


		$jp = str_replace("administrator","",JPATH_BASE);

		$root_file = $jp.".htaccess";
		
		$existingcode = file_get_contents($root_file);
		
		$existingcode_f = str_replace($writingonht_prev,"",$existingcode);
		//$existingcode_f = str_replace($writingonht_prev1,"",$existingcode);
		
		$writingonht = $existingcode_f."RewriteEngine on";
		$writingonht = $writingonht."\n"."RewriteRule ^(.*).(".$ext_n.")$ ".$path."index.php?option=com_download-monitor&file=$1.$2 [R,L]";

		
		if (file_exists($root_file))
		{
			if (is_writable($root_file))
			{
				$handle = fopen($root_file,"w");
				fwrite($handle,$writingonht);
				fclose($handle);
				header("location: index.php?option=com_download-monitor");
			}
			else
			{
				echo "Your .htaccess is not Writable.";
			}
		}
		else
		{
				$handle = fopen($root_file,"w");
				if ($handle)
				{
				fwrite($handle,$writingonht);
				fclose($handle);
				header("location: index.php?option=com_download-monitor");
				}
				else
				{
				echo ".htaccess could not be created, Please create your own.";
				}
		}		
	}
	
?>
            <form action="" method="post" name="editform" onsubmit="return check_edit();">
              <table width="83%" border="0" cellpadding="3" cellspacing="0" class="table">
                <tr> 
                  <td colspan="2" class="Subtitle">Edit Extension </td>
                </tr>
                <tr> 
                  <td width="31%"> 
                    <div align="right">Extension Name<font color="#FF0000"> *</font>                    </div>                  </td>
                  <td width="69%" align="left"> 
                    <input name="extname" type="text" size="50" value="<?php echo $editextname?>">                  </td>
                </tr>
                <tr class="title"> 
                  <td height="20" colspan="2"> 
                    <div align="center"> 
                      <input name="Submit" type="submit" class="button" value="Update">
					  <input name="Submit" type="button" class="button" value="Back" onclick="window.location.href='index.php?option=com_download-monitor'">
                    </div>                  </td>
                </tr>
              </table>
              <br>
            </form>
<?php
}
?>